package io.freespiritsocial.user.ext

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.chad.library.adapter.base.BaseQuickAdapter
import com.google.android.material.tabs.TabLayout
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sm.healthy.R
import com.yanzhenjie.recyclerview.SwipeRecyclerView

/**
 * 描述　:项目中自定义类的拓展函数
 */

fun loadServiceInit(view: View,callback: () -> Unit): LoadService<Any> {
    val loadsir = LoadSir.getDefault().register(view) {
        //点击重试时触发的操作
        callback.invoke()
    }
    loadsir.showSuccess()
    return loadsir
}

fun SmartRefreshLayout.init(onRefresh: () -> Unit,onLoadMore: () -> Unit) {
    setOnRefreshLoadMoreListener(object: OnRefreshLoadMoreListener {
        override fun onLoadMore(refreshLayout: RefreshLayout) {
            onLoadMore.invoke()
        }

        override fun onRefresh(refreshLayout: RefreshLayout) {
            onRefresh.invoke()
        }
    })

}

//绑定SwipeRecyclerView
fun SwipeRecyclerView.init(
    layoutManger: RecyclerView.LayoutManager,
    bindAdapter: RecyclerView.Adapter<*>,
    isScroll: Boolean = true
): SwipeRecyclerView {
    layoutManager = layoutManger
    setHasFixedSize(true)
    adapter = bindAdapter
    isNestedScrollingEnabled = isScroll
    return this
}

//绑定普通的Recyclerview
// FIXME: 2021/12/3  如果有空布局再对空布局进行设置
fun RecyclerView.init(
    bindAdapter: BaseQuickAdapter<*,*>,
    layoutManger: RecyclerView.LayoutManager = LinearLayoutManager(context),
    isScroll: Boolean = true,
    hasEmpty: Boolean = true,
    emptyImage: Int = -1,
    emptyText: String = "",
    emptyLayoutId: Int = -1

): RecyclerView {
    layoutManager = layoutManger
    adapter = bindAdapter
    isNestedScrollingEnabled = isScroll

    if (hasEmpty) {
        /*val view = LayoutInflater.from(context).inflate(emptyLayoutId,this,false)
        view.findViewById<TextView>(R.id.empty_tv)?.text = emptyText
        if (emptyImage > 0)
            view.findViewById<ImageView>(R.id.empty_iv)?.setImageResource(emptyImage)
        bindAdapter.setEmptyView(view)*/
    }
    return this
}


//设置适配器的列表动画
fun BaseQuickAdapter<*,*>.setAdapterAnimation(mode: Int) {
    //等于0，关闭列表动画 否则开启
    if (mode == 0) {
        this.animationEnable = false
    } else {
        this.animationEnable = true
        this.setAnimationWithDefault(BaseQuickAdapter.AnimationType.values()[mode - 1])
    }
}


fun ViewPager2.init(
    fragment: Fragment,
    fragments: ArrayList<Fragment>,
    isUserInputEnabled: Boolean = true
): ViewPager2 {
    //是否可滑动
    this.isUserInputEnabled = isUserInputEnabled
    //设置适配器
    adapter = object: FragmentStateAdapter(fragment) {
        override fun createFragment(position: Int) = fragments[position]
        override fun getItemCount() = fragments.size
    }
    return this
}

fun ViewPager2.initMain(fragment: Fragment,fragments: ArrayList<Fragment>): ViewPager2 {
    //是否可滑动
    this.isUserInputEnabled = false
    this.offscreenPageLimit = 4
    //设置适配器
    adapter = object: FragmentStateAdapter(fragment) {
        override fun createFragment(position: Int): Fragment {
            return fragments[position]
        }

        override fun getItemCount() = fragments.size
    }
    return this
}

fun TabLayout.init(navigationItemSelectedAction: (Int) -> Unit): TabLayout {
    addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener {
        override fun onTabReselected(tab: TabLayout.Tab?) {
        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {
        }

        override fun onTabSelected(tab: TabLayout.Tab?) {
            tab?.let {
                navigationItemSelectedAction.invoke(tab.position)
            }
        }

    })
    return this
}

fun EditText.setOnSearchBtnClick(action: () -> Unit) {
    setOnEditorActionListener { v,actionId,event ->
        if (actionId == EditorInfo.IME_ACTION_SEARCH && length() > 0) {
            action()
            true
        } else
            false
    }
}

/**
 * 隐藏软键盘
 */
fun hideSoftKeyboard(activity: Activity?) {
    activity?.let { act ->
        val view = act.currentFocus
        view?.let {
            val inputMethodManager =
                act.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.hideSoftInputFromWindow(
                view.windowToken,
                InputMethodManager.HIDE_NOT_ALWAYS
            )
        }
    }
}
